package com.billing.service.oss;


import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.apache.log4j.Logger;



public class StaticData
{

	
	private static Logger log = Logger.getLogger(StaticData.class);
	
	private static Map<String, Object> mapStatic = new HashMap<String, Object>();
	
	private static Map<String, Object> oss2WebMap = new HashMap<String, Object>();
	 
	public StaticData(){
		new StaticData.UpdateContentTimer();
	}
	
	public void init() throws Exception{
		
	}
	
	public void setMapStatic(Map<String, Object> mapStatic) throws Exception{
		
		
		this.mapStatic = mapStatic;
	}
	/**
	 * 返回静态容器里的一个子容器的值
	 * @param key
	 * @param subKey
	 * @return
	 */
	public static String getMapValue(String key, String subKey){
		if(mapStatic.containsKey(key.toUpperCase())){
			Map map = (Map)mapStatic.get(key.toUpperCase());
			if(map == null || !map.containsKey(subKey)) return subKey;
			return (String)map.get(subKey);
		}
		else{
			return subKey;
		}
	}
	
	/**
	 * 返回静态容器里的一个子容器的KEY
	 * @param key
	 * @param subValue
	 * @return
	 */
	public static String getMapKey(String key, Object subValue){
		if(mapStatic.containsKey(key.toUpperCase())){
			Map map = (Map)mapStatic.get(key.toUpperCase());
			Iterator it = map.keySet().iterator();
			while(it.hasNext()){
				String k = (String)it.next();
				String v = (String)map.get(k);
				if(v.equals(subValue)){
					return k;
				}
			}
		}
		return null;
	}
	/**
	 * 返回静态容器里的一个子容器
	 * @param key
	 * @return
	 */
	public static Map getMap(String key){
		if(mapStatic.containsKey(key.toUpperCase())){
			Map map = (Map)mapStatic.get(key.toUpperCase());
			return map;
		}
		else{
			return null;
		}
	}
	
	public static Object getObject(String key){
		return mapStatic.get(key.toUpperCase());
	}
	
	
	public static int parseInt(String value,String type){
		if("abs".equals(type))
			return Math.abs(Integer.parseInt(value));
		return Integer.parseInt(value);
	}
	
	public static Map getOss2WebMap(String key){
		if(oss2WebMap.containsKey(key.toUpperCase())){
			Map map = (Map)oss2WebMap.get(key.toUpperCase());
			return map;
		}
		else{
			return null;
		}
	}
	
	/**
	 *  if 1,2,3 => 1  add the entries 1=>1, 2=>1, 3=>1
	 * @param oss2WebMap
	 */
	public void setOss2WebMap(Map<String, Object> oss2WebMap) {
		
		Iterator it = oss2WebMap.keySet().iterator();
		
		while(it.hasNext()){	
			Map<String,String> subMap = (Map)oss2WebMap.get((String)it.next());		
			
			Map addSubMap = new HashMap();
			
			for(String subkey : subMap.keySet()){
				String subValue = subMap.get(subkey);
				String sks[] = subkey.split(",");
				if(sks.length > 1){
					for(String sk : sks){						
						addSubMap.put(sk, subValue);
					}
				}
			}
			subMap.putAll(addSubMap);
		}
		
		StaticData.oss2WebMap = oss2WebMap;
	}	
	
	public static String getOss2WebKey(String key, Object subValue){
		String noSplitKey = null;
		if(oss2WebMap.containsKey(key.toUpperCase())){
			Map submap = (Map)oss2WebMap.get(key.toUpperCase());
			Iterator it = submap.keySet().iterator();
			while(it.hasNext()){			
				String k = (String)it.next();
				String v = (String)submap.get(k);
				if(v.equals(subValue) ){
					if(k.indexOf(",") != -1)
						return k;
					else
						noSplitKey = k;
				}
			}		
		}
		return noSplitKey;			
		
	}	
	
	
	class UpdateContentTimer extends Thread{
		UpdateContentTimer(){
			this.start();
		}
		public void run(){
			while(true){
				try {
					this.sleep(60 * 60 * 1000);
					init();
					log.info("数据更新完毕...");
				} catch (Exception e) {
					log.error("数据更新出错",e);
				}
			}
		}
	}
}
